<script lang="ts" setup>
import PageView from '../../../../components/normal/PageView.vue'
import { onActivated, onBeforeMount, onMounted, onUnmounted, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { initSystemCatch } from '../../../../common/manager/PermissionManager'
import { appState, exitApp } from '../../../../common/manager/appManager'
import { callLater, popMessage } from '../../../../common/utils/normalUtils'

const props = defineProps({
  url: {
    type: String,
    default: ''
  },
  isPage: {
    type: Boolean,
    default: true
  }
})

const systemRef = ref()
const route = useRoute()
const contentInit = ref(false)

/**
 * 发送消息给子
 * @param type
 * @param data
 */
const postMsg = (type:string, data:any) => {
  if (!systemRef.value || !systemRef.value.contentWindow) return
  systemRef.value.contentWindow.postMessage({ type, data }, '*')
}

/**
 * 消息处理
 * @param ev
 */
const winMessageHandler = (ev:any) => {
  const data = ev.data
  if (data.type === 'init') {
    contentInit.value = true
  } else if (data.type === 'getUserInfo') {
    const userInfo = initSystemCatch()
    postMsg('getUserInfo', userInfo)
  } else if (data.type === 'exit') {
    popMessage('登录状态已过期，请重新登录！', 'error')
    exitApp()
  }
}

onBeforeMount(() => {
  window.addEventListener('message', winMessageHandler)
})
onUnmounted(() => {
  window.removeEventListener('message', winMessageHandler)
})

</script>

<template>
  <page-view :padding="false" class="page-inner">
    <iframe ref="systemRef"
            class="system-body"
            width="100%"
            height="100%"
            :src="url" />
  </page-view>
</template>

<style scoped lang="scss">
.page-inner{
  overflow: hidden;
  iframe{
    width:100%;
    height:100%;
    border: none;
  }
}
</style>
